package com.wc.AlgoOJ_train.train1.c3.A202;

import java.util.*;

/**
 * @Author congege
 * @Description 今年暑假不AC
 * http://43.138.190.70:8888/p/A202
 * @Date Created in 2024/1/3-11:10
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Comparator<int[]> comparator = (o1, o2) -> {
            if (o1[0] != o2[0]) {
                return o1[0] - o2[0];
            } else {
                return o1[1] - o2[1];
            }
        };
        int maxN = 10000;
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            if (n == 0) {
                break;
            }
            int[][] origin = new int[n][2];
            for (int i = 0; i < n; i++) {
                origin[i][0] = sc.nextInt();
                origin[i][1] = sc.nextInt();
            }
            // 排序
            Arrays.sort(origin, comparator);
            int[] dp = new int[maxN];
            Arrays.fill(dp, origin[0][1], maxN, 1);
            for (int i = 1; i < n; i++) {
                if (dp[origin[i][0]] + 1 > dp[origin[i][1]]) {
                    Arrays.fill(dp, origin[i][1], maxN, dp[origin[i][0]] + 1);
                }
            }
            System.out.println(dp[maxN - 1]);
        }
    }
}
